from django.core.cache import cache
from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from home.models import NavModel
from home import models,homeser
from home.homeser import NavSerializer
# Create your views here.
class TopView(APIView):
    def get(self,request):
        key = "topnav"
        cachers = cache.get(key)
        if cachers:
            return Response({"massage":"success","data":cachers,"code":200})
        top = models.NavModel.objects.filter(position=True,isshow=True).order_by("order")
        ser = homeser.NavSerializer(top,many=True)
        cache.set(key, ser.data, 60 * 60 * 48)
        return Response({"massge":"success","data":ser.data,"code":200})
class FootView(APIView):
    def get(self,request):
        key = "footdaohang"
        cacheres = cache.get(key)
        if cacheres:
            return Response({"massage":"success","data":cacheres,"code":200})
        top = models.NavModel.objects.filter(position=False,isshow=True).order_by("order")
        ser = homeser.NavSerializer(top,many=True)
        cache.set(key,ser.data,60*60*48)
        return Response({"massge":"success","data":ser.data,"code":200})
class BannerView(APIView):
    def get(self,request):
        key = "bannernav"
        cacheres = cache.get(key)
        if cacheres:
            return Response({"massage": "success", "data": cacheres, "code": 200})
        ban = models.BannerModel.objects.filter(isshow=True).order_by("order")
        ser = homeser.BannerSerializer(ban,many=True)
        cache.set(key, ser.data, 60 * 60 * 12)
        return Response({"massge":"success","data":ser.data,"code":200})

class BannerList(ListAPIView):
    queryset = models.BannerModel.objects.filter(isshow=True).order_by("order")
    serializer_class = homeser.BannerSerializer
    # def get(self,request,):































